<template>
	<div class="page">
		<div class="logo">
			<img class="logo-img" src="../assets/userLogo-2.png" alt="" />
			<div class="right-ic" @click="changeLanguage">
				<img src="../assets/100-1.png" alt="" />
				<span>{{$i18n.locale == 'zh' ? 'EN' : '中文'}}</span>
			</div>
		</div>
		<div class="content">
			<div class="search-wrap">
				<div class="search">
					<input type="text" v-model="search" :placeholder="$t('language.search')" />
					<img class="ic" src="../assets/101.png" alt="" />
				</div>
			</div>
			
			<van-list v-model="loading" :finished="finished" :finished-text="$t('language.noMore')" @load="onLoad">
				<div class="card" v-for="item in list" :key="item.id">
					<div class="user" @click="tapChecked(item)">
						<div class="select">
							<img v-if="item.checked" class="ic" src="../assets/112.png"
								alt="" />
							<img v-else class="ic not" src="../assets/114.png" alt="" />
						</div>
						<div class="user-name">
							{{item.title}}
						</div>
					</div>
				</div>
			</van-list>
		</div>
		
		<div class="add-btn" @click="tapAssignLnr">
			<div class="btn">{{$t('language.text40')}}</div>
		</div>
	</div>
</template>
<script>
	import {
		getHotelLnr,
		// assignLnr
	} from '@/api/user';
	// import {
	// 	Toast 
	// } from "vant";
	let timer = null

	export default {
		name: 'AssignStaff',
		data: function() {
			return {
				locale: 'zh',
				search: '',
				userId: '',
				lnrId: '',
				list: [],
				loading: false,
				finished: false,
				page: 1
			};
		},
		created() {
			const {
				query
			} = this.$route;
			this.lnrId = query.id
		},
		mounted() {},
		watch: {
			search: function() {
				const t = this
				if (timer != null) clearTimeout(timer)
		
				timer = setTimeout(() => {
					timer = null
					clearTimeout(timer)
					t.page = 1
					t.list = []
					t.getHotelLnrData()
				}, 500)
			}
		},
		methods: {
			// tapChecked(item) {
			// 	this.userId = item.id
			// 	this.$forceUpdate()
			// },
			tapChecked(item) {
				if (item.checked) {
					item.checked = false
				} else {
					item.checked = true
				}
				this.$forceUpdate()
			},
			onLoad() {
				this.getHotelLnrData()
			},
			getHotelLnrData() {
				getHotelLnr({
					page: this.page,
					key: this.search
				}).then((res) => {
					let {
						current_page,
						last_page,
						data
					} = res.data
					data.forEach((item)=>{
						item.is_share === 0 ? item.is_share = false : item.is_share = true
					})
					// 加载状态结束
					this.loading = false;
					// 数据全部加载完成
					if (current_page == last_page) {
						this.finished = true;
					} else {
						this.finished = false;
						this.page++
					}
					this.list = [...this.list, ...data]
				}).catch((err) => {
					console.log(err)
					this.loading = false;
				})
			},
			
			tapAssignLnr() {
				const list = this.list
				
				let data = []
				if(list.length > 0) {
					list.forEach((item)=>{
						if(item.checked) {
							data.push(item.id)
						}
					})
				}
				if(data.length == 0) return
				this.$router.push({
					path: 'selectSell',
					query: {
						data
					}
				})
			},
			goPage(page, id) {
				this.$router.push({
					path: page,
					query: {
						id
					}
				})
			},
			changeLanguage() {
				if (this.$i18n.locale == 'zh') {
					this.$i18n.locale = 'en';
					this.$cookies.set('locale', 'en')
				} else {
					this.$i18n.locale = 'zh';
					this.$cookies.set('locale', 'zh')
				}
			}
		}
	};
</script>
<style type="text/css" lang="scss" scoped>
	.page {
		background-color: #f2f5f7;
	}

	.logo {
		background-color: #fff;

		.tab-app {
			display: flex;
			align-items: center;
			font-size: 24px;
			color: #333333;
			img {
				width: 31px;
				height: 30px;
				margin-right: 6px;
			}
		}

		.right-ic {
			color: #333333;
		}
	}

	.content {
		padding-bottom: 80px;

		.search-wrap {
			padding-top: 40px;
			padding-bottom: 16px;
			margin: 0 30px;

			.search {
				position: relative;
				z-index: 10;
				padding: 0 28px;
				height: 74px;
				border: 2px solid #1f4456;
				background-color: #fff;
				box-sizing: border-box;
				border-radius: 45px;
				display: flex;
				align-items: center;
				justify-content: space-between;

				input {
					font-size: 24px;
					color: #666;
					width: 100%;
					height: 100%;
				}

				.ic {
					width: 28px;
					height: 28px;
					flex-shrink: 0;
				}
			}
		}

		.card {
			margin: 24px 30px 0;
			padding: 30px;
			box-sizing: border-box;
			border-radius: 10px;
			background-color: #fff;
			box-shadow: 0 0 24px #eee;
			position: relative;

			.user {
				display: flex;
				align-items: center;
				width: 100%;
				box-sizing: border-box;
				position: relative;
				
				.select {
					position: absolute;
					top: 50%;
					left: 30px;
					transform: translateY(-50%);
					.ic {
						width: 54px;
						height: 54px;
						position: absolute;
						top: 84%;
						right: 0;
						transform: translateY(-50%);
					
						&.not {
							width: 38px;
							height: 38px;
						}
					}
				}
				
				.user-name {
					font-size: 30px;
					font-weight: bold;
					margin-left: 48px;
					color: #333333;
				}
			}
		}
	}

	.add-btn {
		position: fixed;
		left: 50%;
		bottom: 0;
		transform: translateX(-50%);
		z-index: 1000;
		width: 100%;
		height: 114px;
		max-width: 750px !important;
		color: #fff;
		font-size: 28px;
		background-color: #fff;
		box-shadow: 0 2px 24px #eee;
		padding: 14px 30px;
	
		.btn {
			width: 100%;
			padding: 21px 0;
			border-radius: 10px;
			text-align: center;
			background-color: #e8542c;
		}
	}
</style>